package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.Person;
import com.woniuxy.dudugua.entity.User;
import com.woniuxy.dudugua.exception.IncorrectCredentialsException;
import com.woniuxy.dudugua.exception.UnknowAccountException;
import com.woniuxy.dudugua.service.MerchantsService;
import com.woniuxy.dudugua.util.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("person")
public class PersonController {
    @Resource
    private MerchantsService merchantsService;
    /**
     * 用户登录的方法
     */
    @GetMapping("login")
    public Result loginByTel(String tel, String password,HttpSession session) throws Exception {
        Person person = merchantsService.loginByTel(tel);
        if(person==null){
            //不存在用户名
            throw new UnknowAccountException();
        }
        if(!person.getPassword().equals(password)){
            //密码错误
            throw new IncorrectCredentialsException();
        }
        //没报错就成功，然后存入session中
        session.setAttribute("person",person);

        return new Result("200","登录成功",person,null);
    }
}
